<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>信息页面</title>
</head>
<script src="/js/jquery.js"></script>
<script>
    //把接收的 信息存到作用域
    function storeMsg(msg) {
        $.getJSON("/studentManagement/succPage/course/storeMsg","msg="+msg,function (data) {});
    }
    //打印数据
    function printMsg() {
        $.getJSON("/studentManagement/succPage/course/showAllMsg","",function (data) {
            var msg="";
            for(var i=0;i<data.length;i++){
                msg+="<li>"+data[i]+"</li>";
            }
            $("#mymsg").html(msg);
        });
    }

    //站内信
    function siteInbox(){
        var ws=null;
        if('WebSocket' in window){
            ws =new WebSocket("ws://localhost:8080/studentManagement/succPage/socketServer/[[${user.id}]]");
            /////////////////怎么获取发件人///////////////////
            //接收到消息的回调方法
            ws.onmessage = function(event) {
                //存储数据
                storeMsg(event.data);
                //打印数据
                printMsg();
            };
            //关闭站内信的回调方法
            ws.onclose = function(evt) {
            };
            ws.onerror=function(){
            }
            //打开站内信的回调方法
            ws.onopen = function(evt) {
            };
            //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
            window.onbeforeunload = function () {
                closeWebSocket();
            }

            //关闭WebSocket连接
            function closeWebSocket() {
                ws.close();
            }
        }else{
            alert("你的浏览器不支持");
        }
    }
    $(function () {
        //连接站内信
        siteInbox();
        //输出  全部信息
        printMsg();
    });
</script>
<body>
<h3>全部信息</h3>
<ol id="mymsg">

</ol>
<a href="/studentManagement/succPage/win">进入选课首页</a>
</body>
</html>